package com.andon.microcellsimulator.common.socket;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.andon.microcellsimulator.common.util.LogUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 17/12/4.
 */
public class ServerManager {

    private static ServerManager mServerManager;
    private static final Object lock = new Object();
    private ServerCallback mServerCallback;
    private TcpServerThread mTcpServer;
    private UdpServerThread mUdpServer;

    private List<String> mWhiteList;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0:
                    if (mServerCallback != null) {
                        mServerCallback.deviceOnline(msg.obj.toString());
                    }
                    break;
                case 1:
                    if (mServerCallback != null) {
                        mServerCallback.deviceOffline(msg.obj.toString());
                    }
                    break;
                case 2:
                    if (mServerCallback != null) {
                        if (msg.obj instanceof MessageModel) {
                            mServerCallback.recvDeviceMsg(
                                    ((MessageModel) msg.obj).getDeviceID(),
                                    ((MessageModel) msg.obj).getMsg(),
                                    ((MessageModel) msg.obj).getSid());
                        } else {
                            LogUtil.w("CP设备发送的指令格式不正确");
                        }
                    }
                    break;
            }
        }
    };
    
    private ServerManager() {

    }

    public static ServerManager getInstance() {
        if (mServerManager == null) {
            synchronized (lock) {
                if (mServerManager == null) {
                    mServerManager = new ServerManager();
                }
            }
        }

        return mServerManager;
    }

    public void startService() {

        // 开启tcp服务
        startTcpServer();
        // 开启udp服务
        startUdpServer();

    }

    public void stopService() {

        // 停止tcp服务
        stopTcpServer();
        // 停止udp服务
        stopUdpServer();

    }

    public void setServerCallback(ServerCallback callback) {
        mServerCallback = callback;
    }

    public void setWhiteList(List<String> whiteList) {
        mWhiteList = whiteList;

        if (mUdpServer != null) {
            mUdpServer.setWhiteList(mWhiteList);
        }

        if (mTcpServer != null) {
            mTcpServer.setWhiteList(mWhiteList);
        }
    }

    public void sendCommand(String deviceID, String command, String sid) {

        if (mTcpServer.isConnectDevice(deviceID)) {
            mTcpServer.sendCommand(deviceID, command, sid);
        } else {
            LogUtil.w("设备[" + deviceID + "]未在线,发送指令失败");
        }
    }

    public void stopConnect(String did) {
        if (mTcpServer.isConnectDevice(did)) {
            mTcpServer.stopConn(did);
        }
    }

    private void startTcpServer() {

        if (mTcpServer == null) {
            mTcpServer = TcpServerThread.getInstance();
        }

        mTcpServer.setHandler(handler);

        mTcpServer.setWhiteList(mWhiteList);

        mTcpServer.start();

        LogUtil.i("启动tcp服务");

    }

    private void startUdpServer() {

        stopUdpServer();

        if (mUdpServer == null) {
            mUdpServer = new UdpServerThread();
        }

        mUdpServer.setWhiteList(mWhiteList);

        mUdpServer.start();

        LogUtil.i("启动udp服务");
    }

    private void stopTcpServer() {
        if (mTcpServer != null) {
            mTcpServer.cancel(true);
            mTcpServer = null;
        }
    }

    private void stopUdpServer() {
        if (mUdpServer != null) {
            mUdpServer.cancel(true);
            mUdpServer = null;
        }
    }

}
